package com.douguo.recipe.sina;

import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;

import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.douguo.recipe.sina.WeiboHttpClient.WeiboResponseCallBack;

/**
 * 封装微博的步骤性的方法
 * 
 * @author zuozp
 * 
 */
public class WeiboHelper {
	public static final String CONSUMER_KEY = "1008754100";
	public static final String CONSUMER_SECRET = "aad61b1c44355c1337858d5dd29e6f98";
	public static final String WEIBO_OAUTH_CALLBACK_KEY = "weibo_callback";
	public static final String WEIBO_CAR_ACTIVITY_KEY = "weibo_car_activity";
	/**
	 * 该回调url需要指向返回结果后需要启动的Activity
	 */
	private static final String CALLBACK_URL = "douguo://OAuthActivity";

	/**
	 * 请求requestToken
	 */
	public static void reqRequestToken(Context context,  
			WeiboResponseCallBack callBack) {
		final String url = "http://api.t.sina.com.cn/oauth/request_token";
		PostParameter[] postParams = new PostParameter[] { 
				new PostParameter("oauth_callback", CALLBACK_URL) 
				};
		WeiboHttpClient httpClient = new WeiboHttpClient(context, url,
				postParams, callBack);
		httpClient.start();
	}

	/**
	 * 进行用户认证
	 * 
	 * @param context
	 * @param pin
	 * @param callBack
	 */
	public static void reqAccessToken(Context context, RequestToken reqToken,
			String pin, WeiboResponseCallBack callBack) {
		final String url = "http://api.t.sina.com.cn/oauth/access_token";  
		PostParameter[] postParams = new PostParameter[] { new PostParameter(
				"oauth_verifier", pin) };

		WeiboHttpClient httpClient = new WeiboHttpClient(context, url,
				postParams, reqToken, callBack);
		httpClient.start();
	}

	/**
	 * 发布微博信息
	 * 
	 * @param context
	 * @param accessToken
	 * @param msg
	 */
	public static void updateStatus(Context context, AccessToken accessToken,
			String msg, WeiboResponseCallBack callBack) {
		final String url = "http://api.t.sina.com.cn/statuses/update.json";
		PostParameter[] postParams = new PostParameter[] { new PostParameter(
				"status", msg) };

		WeiboHttpClient httpClient = new WeiboHttpClient(context, url,
				postParams, accessToken, callBack);
		httpClient.start();
	}

	/**
	 * upload the user's status. The text will be trimed if the length of the
	 * text is exceeding 160 characters. The image suport. <br>
	 * This method calls http://api.t.sina.com.cn/statuses/upload.format
	 * 
	 * @param status
	 *            the text of your status update
	 * @return the latest status
	 * @throws WeiboException
	 *             when Weibo service or network is unavailable
	 * @since Weibo4J 1.1220
	 * @see <a
	 *      href="http://open.t.sina.com.cn/wiki/index.php/Statuses/upload">statuses/upload
	 *      </a>
	 */
	public static void uploadStatus(Context context, AccessToken accessToken,
			Bitmap bitmap, String status, WeiboResponseCallBack callBack) {

		String url = "http://api.t.sina.com.cn/statuses/upload.json";
		PostParameter[] postParams = new PostParameter[] {
				new PostParameter("status", URLEncoder.encode(status)),
				new PostParameter("source", CONSUMER_KEY) };

		// WeiboHttpClient httpClient = new WeiboHttpClient(context, url,
		// postParams, accessToken, callBack, true);
		// httpClient.start();

		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bitmap.compress(CompressFormat.JPEG, 100, baos);

			ImageItem img = new ImageItem("pic", baos.toByteArray());

			Response res = WeiboHttpClient.upload(context, url, accessToken,
					CONSUMER_KEY, CONSUMER_SECRET, postParams, img, true);

			// {"request":"/statuses/upload.json","error_code":"401","error":"40107:Oauth Error: signature_invalid!"}

				JSONObject jsobj = res.asJSONObject();
				if (jsobj.has("error_code")){
					int errorCode = jsobj.getInt("error_code");
					callBack.onException(errorCode);
				} else {
					callBack.onResponse(res);
				}
		} catch (Exception e) {
			e.printStackTrace();
			callBack.onException(401);
		}

	}
}
